<?php
defined ( 'IN_TS' ) or die ( 'Access Denied.' );

/**
 * 分组列表【api接口示例】
 * index.php?app=group&ac=api&api=all
 * get
 *
 * @cateid  分类ID
 * @page
 * @limit
 * 
 */

$all= isset($_GET['all']) ? intval($_GET['all']) : 1;
$type= intval($_GET['type']); // 排序类型 
$province = isset($_GET['province']) ? $_GET['province'] : ''; // 省份
$city = isset($_GET['city']) ? $_GET['city'] : ''; // 城市
$status = isset($_GET['status']) ? $_GET['status'] : ''; // 是否停办，0 已停办 1 筹备中 2 运营中
$isassist = isset($_GET['isassist']) ? $_GET['isassist'] : ''; // 微澜主办还是协办
$iscouncil = isset($_GET['iscouncil']) ? $_GET['iscouncil'] : ''; // 是否成立理事会


// 所有小组
$url = tsUrl ( 'group', 'index', array ('all'=>$all ));
$order = 'isrecommend desc,';

if($type){
	switch($type){
		case 1:
			if(!$all){$all=1;}
			$order =$order.'count_sto asc,';
			break;
		case 2:
			if(!$all){$all=1;}
			$order =$order.'count_active asc,count_lastduty asc,count_nextduty asc,';
			break;
		case 3:
			if(!$all){$all=3;}
			$order =$order.'count_active,';
			break;
	}
}

$arr = 'isaudit=0';

switch($all){
	case 2:
		$myGroup = array ();
		if ($TS_USER ['userid']) {
			$myGroups = $new ['group']->findAll ( 'group_user', array (
				'userid' => $TS_USER ['userid']
			), null, 'groupid' );
			foreach ( $myGroups as $item ) {
				$myGroup [] = $item ['groupid'];
			}
			$str = implode(',',$myGroup);
			if($str){
				$arr = $arr.' and groupid in('.$str.') ';
			}
		}

		break;
	case 1:
		$arr = $arr.' and isplace=2';
		$order = $order.'joinway asc,status asc,';
		break;
	case 3:
		$arr = $arr.' and isplace=1';
		break;

}
$order = $order.'addtime asc';

if($all==1||$all==3){
	if($type==1){
		$arr=$arr.' and isdonate=1 and count_sto<40';
	}
	if($type==3||$type==2){
		$arr=$arr.' and joinway = 0';
	}
}

if($province){
	$arr = $arr.' and province="'. $province.'"';
}

if($city){
	$arr = $arr.' and city="'. $city.'"';
}

if($status){
	if($status==1){
		$arr = $arr.' and status = 1';
	} else if($status==2){
		$arr = $arr.' and status = 2';
	}else{
		$arr = $arr.' and status = 0';
	}
}

if($isassist){
	if($isassist==1){
		$arr = $arr.' and isassist = 1';
	}else{
		$arr = $arr.' and isassist = 0';
	}
}
if($iscouncil){
	if($iscouncil==1){
		$arr = $arr.' and iscouncil = 1';
	}else{
		$arr = $arr.' and iscouncil = 0';
	}
}

$strAll =  $new ['group']->findAll ('group',array('isplace'=>2));
foreach($strAll as $v){
	if(!in_array($v['province'],$strPro)){
		$strPro[] = $v['province'];
	}
	if($province==$v['province']){
		if(!in_array($v['city'],$strCity)){
			$strCity[] = $v['city'];
		}
	}

}

$arrGroup = $new ['group']->findAll ( 'group', $arr, $order, null );

foreach ( $arrGroup as $key => $item ) {
	$arrGroup [$key] ['groupname'] = tsTitle ( $item['groupname'] );
	$strOpen = explode(',',$item['openday']);

	// 分割处理经纬度 langitude 经度，latitude 纬度	
	if (empty($item['coordinate'])) {
		$arrGroup [$key] ['coordinate'] = [
			"longitude" => null,
			"latitude" => null,
		];
	} else {
		$coordinates = explode(",", $item["coordinate"]);
		$arrGroup [$key] ['coordinate'] = [
			"longitude" => $coordinates[0],
			"latitude" => $coordinates[1],
		];
	}

	$openday='';
	foreach($strOpen as $v){
		if(!$openday){
			$openday = num2day($v);
		}else{
			$openday = $openday.num2day($v);
		}
	}
	$arrGroup [$key] ['openday'] = $openday;


	if(!$all){
		$arrGroup [$key] ['groupdesc'] = $item ['province'].$item ['city'].$item ['address'];
	}else{
		$arrGroup [$key] ['groupdesc'] = cututf8 ( t(tsDecode($item ['groupdesc'])), 0, 30 );
	}

	if($all==1||$all==3){


		if($all==1){
			$strDuty = aac('duty')->find('duty','groupid='.$item['groupid'].' and status=1 and cateid=1 and addtime>'.strtotime('-7 day'));
			if(!$strDuty&&$arrGroup[$key]['count_active']<6&&$arrGroup[$key]['status']==0&&$arrGroup[$key]['joinway']==0){
				$arrGroup[$key]['sick']=1;
			}else{
				$arrGroup[$key]['sick']=0;
			}
		}
	}
}


$jsonData = json_encode(array(

    'status'=> 1,
    'msg'=> 'success',
    'data'=> $arrGroup,
));

if($_GET['callback']){
    echo $_GET['callback'].'('.$jsonData.')';
    exit;
}else{
    echo $jsonData;
}